package com.demo;

import com.google.gson.Gson;

import java.io.IOException;

/**
 * @author WuLiangHang
 */
@javax.servlet.annotation.WebServlet(name = "AjaxDemoServlet", urlPatterns = "/ajax01.do")
public class AjaxDemoServlet extends javax.servlet.http.HttpServlet {
    @Override
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        System.out.println("传来的参数：" + request.getParameter("action"));
        Person p = new Person(1, 18, "小明", 'M');
        Gson gson = new Gson();
        String jsonStr = gson.toJson(p);
        response.setContentType("text/html;charset=utf-8");
        //将json字符串传到前端去
        response.getWriter().write(jsonStr);
    }
}
